package bold.storage.splice;


import java.util.*;

public class SqlKit {
    private static String pk = "id";

    public SqlKit() {
    }

    public static String getPk() {
        return pk;
    }

    public static void setPk(String primaryKey) {
        pk = primaryKey;
    }

    public static String wrapCol(String word) {
        if (!word.contains(".")) {
            return "`" + word + "`";
        } else {
            StringBuilder sb = new StringBuilder();
            String[] var2 = word.split("\\.");
            int var3 = var2.length;

            for (int var4 = 0; var4 < var3; ++var4) {
                String p = var2[var4];
                sb.append("`").append(p).append("`").append(".");
            }

            sb.deleteCharAt(sb.lastIndexOf("."));
            return sb.toString();
        }
    }

    public static String wrapBlank(String word) {
        return " " + word + " ";
    }

    private static String[] concatForInsert(Set<String> keySet) {
        StringBuilder keys = new StringBuilder();
        StringBuilder values = new StringBuilder();
        Iterator var3 = keySet.iterator();

        while (var3.hasNext()) {
            String key = (String) var3.next();
            keys.append(wrapCol(key)).append(",");
            values.append("?").append(",");
        }

        keys = wrapCurve(StrKit.removeLastComma(keys));
        values = wrapCurve(StrKit.removeLastComma(values));
        return new String[]{keys.toString(), values.toString()};
    }

    private static String concatForUpdate(Set<String> keySet) {
        StringBuilder sb = new StringBuilder();
        Iterator var2 = keySet.iterator();

        while (var2.hasNext()) {
            String key = (String) var2.next();
            //if (!"id".equals(key)) {
            sb.append(wrapCol(key)).append("=?").append(",");
            //}
        }

        return StrKit.removeLastComma(sb).toString();
    }

    public static String concatLong(Long... longs) {
        return StrKit.concat(null, String.valueOf(longs));
    }

    public static String concatString(String... str) {
        return StrKit.concat("'", str);
    }

    public static String concatCol(String... str) {
        return StrKit.concat("`", str);
    }

    public static String concat(String... str) {
        return StrKit.concat(null, str);
    }

    public static String wrapCurve(String str) {
        return "(" + str + ")";
    }

    public static StringBuilder wrapCurve(StringBuilder sb) {
        return sb.insert(0, "(").append(")");
    }

    public static Object[] join(Object o, Collection<Object> c) {
        ArrayList<Object> list = new ArrayList<>();
        list.add(o);
        list.addAll(c);
        return list.toArray();
    }

    public static Object[] join(Collection<Object> c1, Collection<Object> c2) {
        ArrayList<Object> list = new ArrayList<>();
        list.addAll(c1);
        list.addAll(c2);
        return list.toArray();
    }

    public static String generateInsertSql(String table, Map<String, Object> propMap) {
        StringBuilder sql = new StringBuilder();
        String[] kv = concatForInsert(propMap.keySet());
        sql.append("insert into ").append(wrapCol(table)).append(" ").append(kv[0]).append(" values ").append(kv[1]);
        return sql.toString();
    }

    public static String generateReplaceSql(String table, Map<String, Object> propMap) {
        StringBuilder sql = new StringBuilder();
        String[] kv = concatForInsert(propMap.keySet());
        sql.append("replace into ").append(wrapCol(table)).append(" ").append(kv[0]).append(" values ").append(kv[1]);
        return sql.toString();
    }

    public static String generateUpdateSql(String table, Map<String, Object> propMap, String where) {
        StringBuilder sql = new StringBuilder();
        String setPart = concatForUpdate(propMap.keySet());
        sql.append("update ").append(wrapCol(table)).append(" set ").append(setPart).append(where);
        return sql.toString();
    }

    public static String generateDeleteSql(String table, String where) {
        StringBuilder sql = new StringBuilder();
        sql.append("delete from ").append(wrapCol(table)).append(where);
        return sql.toString();
    }

    public static String concatPlaceholder(int size) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < size; ++i) {
            sb.append('?').append(',');
        }

        return StrKit.removeLastComma(sb).toString();
    }

    public static String generateIncreaseSql(boolean isIncrease, String table, String col, String where) {
        StringBuilder sql = new StringBuilder();
        sql.append("update ").append(wrapCol(table)).append(" set ").append(wrapCol(col)).append(" = ").append(wrapCol(col)).append(isIncrease ? "+" : "-").append("?").append(where);
        return sql.toString();
    }

    public static String as(String sql, String as) {
        return wrapCurve(sql) + " as " + as;
    }
}
